import {queryCurrent, queryUsers} from "../../services/user";
import {SAVE_CURRENT_USER, USER_FETCH, USER_FETCH_CURRENT, USER_SAVE} from "./const";
import {call, put, take} from "redux-saga/effects";


function* fetch(params) {
  const rsp = yield call(queryUsers, params);
  yield put({
    type: USER_SAVE,
    payload: rsp.data,
  })
}

function* fetchCurrent(params) {
  const rsp = yield call(queryCurrent, params);
  yield put({
    type: SAVE_CURRENT_USER,
    payload: rsp.data,
  })
}


export default function* saga() {
  while (true) {
    const {type, params} = yield take([USER_FETCH, USER_FETCH_CURRENT]);
    try {
      if (type === USER_FETCH) {
        yield call(fetch, params);
      } else {
        yield call(fetchCurrent, params);
      }
    } catch (e) {
      console.log(e);
    }
  }
}
